package com.shark.springbootcms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shark.springbootcms.common.JwtUtils;
import com.shark.springbootcms.common.Result;
import com.shark.springbootcms.common.ResultCode;
import com.shark.springbootcms.form.UserForm;
import com.shark.springbootcms.mapper.RoleMapper;
import com.shark.springbootcms.mapper.UserMapper;
import com.shark.springbootcms.model.User;
import com.shark.springbootcms.service.UserService;
import com.shark.springbootcms.utils.IdWorker;
import com.shark.springbootcms.vo.RolesVo;
import com.shark.springbootcms.vo.UserRoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class UserServiceImpl implements UserService {


    @Autowired
    private IdWorker idWorker;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result register(User user) {
        user.setId(String.valueOf(idWorker.nextId()));
        userMapper.register(user);
        Result success = Result.SUCCESS();
        return success;
    }

    @Override
    public Result login(User user) {
        User u = userMapper.selectUserByMobile(user.getMobile(),user.getPassword());
        if(u == null) {
            return Result.ERROR();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("name",u.getName());
        String token = jwtUtils.createJwt(u.getId(), u.getName(), map);
        return new Result(ResultCode.SUCCESS,token);
    }

    @Override
    public Result list(UserForm form) {
        PageHelper.startPage(form.getPage(),form.getPageSize());
        List<UserRoleVo> users = userMapper.userList(form);
        PageInfo<UserRoleVo> info = new PageInfo<> (users);
        return new Result(ResultCode.SUCCESS,info);
    }

    @Override
    public Result add(User user) {
        String userId = String.valueOf(idWorker.nextId());
        user.setId(userId);
        //新增用户
        userMapper.register(user);
        //新增用户角色
        userMapper.insertUserRole(userId,user.getRoles());
        Result success = Result.SUCCESS();
        return success;
    }

    @Override
    public Result edit(User user, String id) {
        user.setId(id);
        //修改角色
        userMapper.editUser(user);
        //删除用户角色
        userMapper.deleteUserRole(id);
        //新建用户角色关系
        userMapper.insertUserRole(id,user.getRoles());
        Result success = Result.SUCCESS();
        return success;
    }

    @Override
    public Result delete(String id) {
        //删除用户
        userMapper.deleteUserById(id);
        //删除用户角色
        userMapper.deleteUserRole(id);
        Result success = Result.SUCCESS();
        return success;
    }

    @Override
    public Result roles() {
        List<RolesVo> roles = roleMapper.selectRoles();
        return Result.SUCCESS(roles);
    }
}
